package com.es.spring_es.rabbitmq.config;


import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class RabbitMQConfig {

    @Value("${spring.rabbitmq.host}")
    private String address;
    @Value("${spring.rabbitmq.port}")
    private int port;
    @Value("${spring.rabbitmq.username}")
    private String userName;
    @Value("${spring.rabbitmq.password}")
    private String passWord;

/*

    @Value("${spring.rabbitmq.beijing.host}")
    private String bjaddress;
    @Value("${spring.rabbitmq.beijing.port}")
    private int bjport;
    @Value("${spring.rabbitmq.beijing.username}")
    private String bjuserName;
    @Value("${spring.rabbitmq.beijing.password}")
    private String bjpassWord;
*/





    @Bean(name="shanghaiConnectionFactory")
    @Primary
    public ConnectionFactory shanghaiConnectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setAddresses(address);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(userName);
        connectionFactory.setPassword(passWord);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

    @Bean(name="shanghairabbitTemplate")
    @Primary
    public RabbitTemplate shanghairabbitTemplate(@Qualifier("shanghaiConnectionFactory") ConnectionFactory shanghaiConnectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(shanghaiConnectionFactory);
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }

    @Bean(name = "shanghaiACK")
    public RabbitListenerContainerFactory shanghaiACK(ConnectionFactory shanghaiConnectionFactory){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(shanghaiConnectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }

    @Bean(name = "shanghaiNACK")
    public RabbitListenerContainerFactory shanghaiNACK(ConnectionFactory shanghaiConnectionFactory){
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(shanghaiConnectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
        return factory;
    }


}
